package com.reeygu.item.controller;

import com.reeygu.item.service.UserService;
import com.reeygu.item.vo.LoginVo;
import com.reeygu.item.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Slf4j
@Controller
@RequestMapping("/swagger/login")
//设置大接口标题，tags可有多个
@Api(tags = {"LoginController"}, description = "登录接口")
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 跳往登录页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * @Valid 即使前端有校验，后端也要重新校验一次，这样是避免恶意用户跳过前端直接调接口导致服务器报错
     * @return
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    @ApiOperation(value = "登录操作", notes = "用户登录接口")
    public RespBean doLogin(@Valid LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        return userService.doLogin(loginVo, request, response);
    }
}
